import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

// 宠物实体类
class Pet {
    private String petId;
    private String name;
    private String breed;
    private int age;
    private String ownerId;

    public Pet(String petId, String name, String breed, int age, String ownerId) {
        this.petId = petId;
        this.name = name;
        this.breed = breed;
        this.age = age;
        this.ownerId = ownerId;
    }

    public String getPetId() {
        return petId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getBreed() {
        return breed;
    }

    public int getAge() {
        return age;
    }

    public String getOwnerId() {
        return ownerId;
    }

    @Override
    public String toString() {
        return "宠物id =" + petId + "\t姓名=" + name + "\t品种=" + breed + "\t年龄=" + age;
    }
}

// 主人实体类
class Owner {
    private String ownerId;
    private String name;
    private String phone;
    private String address;

    public Owner(String ownerId, String name, String phone, String address) {
        this.ownerId = ownerId;
        this.name = name;
        this.phone = phone;
        this.address = address;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString() {
        return "主人id =" + ownerId + "\t姓名=" + name + "\t电话号码=" + phone + "\t地址=" + address;
    }
}

// 医疗记录实体类
class MedicalRecord {
    private String recordId;
    private String petId;
    private String type;
    private LocalDate date;
    private String description;

    public MedicalRecord(String recordId, String petId, String type, LocalDate date, String description) {
        this.recordId = recordId;
        this.petId = petId;
        this.type = type;
        this.date = date;
        this.description = description;
    }

    public String getRecordId() {
        return recordId;
    }

    public String getPetId() {
        return petId;
    }

    public String getType() {
        return type;
    }

    public LocalDate getDate() {
        return date;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return "医疗id=" + recordId + "\t类型=" + type + "\t接种时间=" + date;
    }
}

// 宠物管理系统主类
public class PetManagementSystemGUI extends JFrame {
    private static List<Pet> pets = new ArrayList<>();
    private static Map<String, Owner> owners = new HashMap<>();
    private static List<MedicalRecord> medicalRecords = new LinkedList<>();

    static {
        owners.put("o101", new Owner("o101", "张三", "17788994455", "上海市外滩"));
        owners.put("o102", new Owner("o102", "李四", "09999999999", "北京市海淀区"));
        owners.put("o103", new Owner("o103", "王五", "10099885166", "昌吉市融投"));
    }

    private JTextField nameField;
    private JTextField passwordField;
    private JPanel mainPanel;

    public PetManagementSystemGUI() {
        setTitle("宠物管理系统");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 登录界面
        JPanel loginPanel = new JPanel();
        loginPanel.setLayout(new GridLayout(3, 2));
        JLabel nameLabel = new JLabel("姓名:");
        nameField = new JTextField();
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JTextField();
        JButton loginButton = new JButton("登录");

        loginPanel.add(nameLabel);
        loginPanel.add(nameField);
        loginPanel.add(passwordLabel);
        loginPanel.add(passwordField);
        loginPanel.add(new JLabel());
        loginPanel.add(loginButton);

        add(loginPanel);

        // 登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                String password = passwordField.getText();
                if (name.equals("张三") && password.equals("123456")) {
                    showMainMenu();
                } else {
                    JOptionPane.showMessageDialog(null, "登陆失败，重新登陆");
                }
            }
        });
    }

    private void showMainMenu() {
        getContentPane().removeAll();
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(6, 1));

        JButton petManagementButton = new JButton("宠物信息管理");
        JButton ownerManagementButton = new JButton("主人信息管理");
        JButton medicalRecordButton = new JButton("医疗记录管理");
        JButton statisticsButton = new JButton("统计查询");
        JButton exitButton = new JButton("退出系统");

        mainPanel.add(petManagementButton);
        mainPanel.add(ownerManagementButton);
        mainPanel.add(medicalRecordButton);
        mainPanel.add(statisticsButton);
        mainPanel.add(exitButton);

        add(mainPanel);
        revalidate();
        repaint();

        // 宠物信息管理按钮事件
        petManagementButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPetManagementMenu();
            }
        });

        // 退出系统按钮事件
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "感谢使用，系统退出");
                System.exit(0);
            }
        });
    }

    private void showPetManagementMenu() {
        getContentPane().removeAll();
        JPanel petPanel = new JPanel();
        petPanel.setLayout(new GridLayout(5, 1));

        JButton addPetButton = new JButton("新增宠物");
        JButton updatePetButton = new JButton("编辑宠物");
        JButton deletePetButton = new JButton("删除宠物");
        JButton queryPetButton = new JButton("查询宠物");
        JButton backButton = new JButton("返回主菜单");

        petPanel.add(addPetButton);
        petPanel.add(updatePetButton);
        petPanel.add(deletePetButton);
        petPanel.add(queryPetButton);
        petPanel.add(backButton);

        add(petPanel);
        revalidate();
        repaint();

        // 新增宠物按钮事件
        addPetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addPet();
            }
        });

        // 编辑宠物按钮事件
        updatePetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updatePet();
            }
        });

        // 删除宠物按钮事件
        deletePetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deletePet();
            }
        });

        // 查询宠物按钮事件
        queryPetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryPets();
            }
        });

        // 返回主菜单按钮事件
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showMainMenu();
            }
        });
    }

    private void addPet() {
        String petId = JOptionPane.showInputDialog("输入宠物ID(pXXX): ");
        if (petId == null) return;
        if (!isValidPetId(petId)) {
            JOptionPane.showMessageDialog(null, "宠物ID格式错误，应为p+3位数字");
            return;
        }
        String name = JOptionPane.showInputDialog("输入宠物姓名: ");
        String breed = JOptionPane.showInputDialog("输入品种: ");
        String ageStr = JOptionPane.showInputDialog("输入年龄: ");
        int age;
        try {
            age = Integer.parseInt(ageStr);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "错误：年龄必须为数字");
            return;
        }
        String ownerId = JOptionPane.showInputDialog("输入主人ID(oXXX): ");
        if (!isValidOwnerId(ownerId)) {
            JOptionPane.showMessageDialog(null, "主人ID格式错误，应为O+3位数字");
            return;
        }
        if (!owners.containsKey(ownerId)) {
            JOptionPane.showMessageDialog(null, "错误：主人" + ownerId + "不存在");
            return;
        }
        pets.add(new Pet(petId, name, breed, age, ownerId));
        JOptionPane.showMessageDialog(null, "添加成功");
    }

    private void updatePet() {
        String petId = JOptionPane.showInputDialog("输入要修改的宠物ID: ");
        if (petId == null) return;
        Pet pet = findPetById(petId);
        if (pet == null) {
            JOptionPane.showMessageDialog(null, "错误：宠物不存在");
            return;
        }
        String name = JOptionPane.showInputDialog("输入新姓名(当前: " + pet.getName() + "): ");
        String breed = JOptionPane.showInputDialog("输入新品种(当前: " + pet.getBreed() + "): ");
        String ageStr = JOptionPane.showInputDialog("输入新年龄(当前: " + pet.getAge() + "): ");
        int age;
        try {
            age = Integer.parseInt(ageStr);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "错误：年龄必须为数字");
            return;
        }
        pet.setName(name);
        pet.setBreed(breed);
        pet.setAge(age);
        JOptionPane.showMessageDialog(null, "修改成功：宠物" + petId + "信息已更新");
    }

    private void deletePet() {
        String petId = JOptionPane.showInputDialog("输入宠物ID: ");
        if (petId == null) return;
        Iterator<Pet> iterator = pets.iterator();
        while (iterator.hasNext()) {
            Pet pet = iterator.next();
            if (pet.getPetId().equals(petId)) {
                iterator.remove();
                JOptionPane.showMessageDialog(null, "删除成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null, "错误：宠物不存在");
    }

    private void queryPets() {
        String keyword = JOptionPane.showInputDialog("输入查询条件(空表示查询全部):(PetId/name/OwnerId) ");
        if (keyword == null) return;
        keyword = keyword.toLowerCase();
        List<Pet> results = new ArrayList<>();
        for (Pet pet : pets) {
            if (pet.getPetId().toLowerCase().contains(keyword) ||
                    pet.getName().toLowerCase().contains(keyword) ||
                    pet.getOwnerId().toLowerCase().contains(keyword)) {
                results.add(pet);
            }
        }
        if (results.isEmpty()) {
            JOptionPane.showMessageDialog(null, "未找到匹配结果");
        } else {
            StringBuilder sb = new StringBuilder("匹配结果:\n");
            for (Pet pet : results) {
                sb.append(pet).append("\n");
            }
            JOptionPane.showMessageDialog(null, sb.toString());
        }
    }

    private boolean isValidPetId(String petId) {
        return petId.matches("p\\d{3}");
    }

    private boolean isValidOwnerId(String ownerId) {
        return ownerId.matches("o\\d{3}");
    }

    private Pet findPetById(String petId) {
        for (Pet pet : pets) {
            if (pet.getPetId().equals(petId)) {
                return pet;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                PetManagementSystemGUI gui = new PetManagementSystemGUI();
                gui.setVisible(true);
            }
        });
    }
}